﻿#include "housemanage.h"
#include "ui_housemanage.h"
#include <QMessageBox>

HouseManage::HouseManage(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::HouseManage)
{
    ui->setupUi(this);

    m_contentCount = 0;
    m_houseEdit = nullptr;
}

HouseManage::~HouseManage()
{
    delete ui;
}

void HouseManage::setHotelID(const QString& hotelID)
{
    m_hotelID = hotelID;
}
QString HouseManage::getHotelID()
{
    return m_hotelID;
}

void HouseManage::setHouseList()
{
    HouseManageContent *houseContent = new HouseManageContent();
    m_houseVector.append(houseContent);
    m_houseImageVector.append(ImageList());
    ui->lwg_houseTypeList->addItem("");
    ui->lwg_houseTypeList->setItemWidget(ui->lwg_houseTypeList->item(m_contentCount),houseContent);
    ui->lwg_houseTypeList->item(m_contentCount)->setSizeHint(QSize(0, 120));  //每次改变Item的高度
    m_contentCount++;

    connect(houseContent,SIGNAL(signalWatchHouseTypeInfo(const HouseInfo&)),
            this, SLOT(slotWatchHouseTypeInfo(const HouseInfo&)));
    connect(houseContent,SIGNAL(signalDeleteHouseTypeInfo(const QString&)),
            this, SIGNAL(signalDeleteHouseType(const QString&)));

    ui->lb_houseCount->setText(QString::number(m_contentCount));
}

void HouseManage::saveHouseImage(const QString& houseType,const QImage& image)
{
    for (int i = 0; i < m_contentCount; i++)
    {
        if (houseType == m_houseVector[i]->getHouseType())
        {
            m_houseImageVector[i].append(image);
            if (m_houseImageVector[i].size() == 1)
            {
                m_houseVector[i]->setHouseImage(image);
            }
        }
    }
}

void HouseManage::on_pb_addHouse_clicked()
{
    if (m_houseEdit != nullptr)
    {
        delete m_houseEdit;
    }
    m_houseEdit = new HouseManageEdit();
    m_houseEdit->setHotelID(m_hotelID);
    m_houseEdit->setState(E_NewCreate);
    m_houseEdit->setIndex(m_contentCount);
    m_houseEdit->setWindowTitle("新增房间类型");
    m_houseEdit->setWindowModality(Qt::ApplicationModal);
    m_houseEdit->setWindowFlags(windowFlags()&~Qt::WindowMaximizeButtonHint); // 禁止最大化按钮
    m_houseEdit->setFixedSize(m_houseEdit->width(),m_houseEdit->height());   // 禁止拖动窗口大小
    m_houseEdit->show();

    connect(m_houseEdit,SIGNAL(signalSubmitHouseInfo()),
            this, SIGNAL(signalSubmitHouseType()));
}

void HouseManage::slotWatchHouseTypeInfo(const HouseInfo& houseInfo)
{
    if (m_houseEdit != nullptr)
    {
        delete m_houseEdit;
    }
    m_houseEdit = new HouseManageEdit();
    m_houseEdit->setState(E_WatchDetails);
    m_houseEdit->setWindowTitle("查看房间类型");
    m_houseEdit->setWindowModality(Qt::ApplicationModal);
    m_houseEdit->setWindowFlags(windowFlags()&~Qt::WindowMaximizeButtonHint); // 禁止最大化按钮
    m_houseEdit->setFixedSize(m_houseEdit->width(),m_houseEdit->height());   // 禁止拖动窗口大小
    m_houseEdit->setHouseInfosShow(houseInfo);
    for (int i = 0; i < m_contentCount; i++)
    {
        if (houseInfo.getType() == m_houseVector[i]->getHouseType())
        {
            m_houseEdit->setHouseImageShow(m_houseImageVector[i].size(),m_houseImageVector[i]);
        }
    }

    connect(m_houseEdit,SIGNAL(signalUpdateHouseInfo()),
            this, SIGNAL(signalSubmitHouseType()));
    m_houseEdit->show();
}

void HouseManage::cleanHouseInfoList()
{
    while (ui->lwg_houseTypeList->count() > 0)
    {
        QListWidgetItem * item = ui->lwg_houseTypeList->takeItem(0);
        delete item;
    }

    for(int i = 0; i < 2; i++)
    {
        m_contentCount = 0;
        m_houseVector.clear();
        m_houseImageVector.clear();
    }
    ui->lb_houseCount->setText("0");
}

void HouseManage::setSubmitHouseResult(bool result,const QString& msg)
{
    if (result)
    {
        m_houseEdit->close();
    }
    else
    {
        errorShow(msg);
    }
}

void HouseManage::errorShow(const QString& msg)
{
    QMessageBox::warning(m_houseEdit, "错误", msg,  QMessageBox::Yes, QMessageBox::Yes);
}
